<%#
 Copyright 2013-2025 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
<%#
 This is a fragment file, it will be merged into the root template if available.
 EJS fragments will process % delimiter tags in the template and & delimiter tags during the merge process.
-%>
<&_ if (fragment.staticImportSection) { -&>
<%_ if (!embedded) { _%>
import static <%= packageName %>.config.Constants.ID_DELIMITER;
import static <%= entityAbsoluteClass %>.TYPE_NAME;
import static org.springframework.data.couchbase.core.mapping.id.GenerationStrategy.UNIQUE;
<%_ } _%>
<&_ } -&>

<&_ if (fragment.importSection) { -&>
  <%_ if (!embedded) { _%>
import org.springframework.data.annotation.Id;
import org.springframework.data.couchbase.core.mapping.id.GeneratedValue;
import org.springframework.data.couchbase.core.mapping.Document;
import org.springframework.data.couchbase.repository.Collection;
import org.springframework.data.annotation.TypeAlias;
  <%_ } _%>
import org.springframework.data.couchbase.core.mapping.Field;
  <%_ if (relationships.length > 0) { _%>
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.util.stream.Collectors;
  <%_ } _%>
<&_ } -&>

<&_ if (fragment.annotationSection) { -&>
  <%_ if (!embedded) { _%>
@Document
@TypeAlias(TYPE_NAME)
@Collection(TYPE_NAME)
  <%_ } _%>
<&_ } -&>

<&_ if (fragment.classStaticFieldsSection) { -&>
  <%_ if (!embedded) { _%>
    public static final String TYPE_NAME = "<%= entityInstance %>";
  <%_ } _%>
<&_ } -&>

<%_ for (const field of fields) { _%>
<&_ if (fragment.field<%- field.fieldNameCapitalized %>AnnotationSection) { -&>
  <%_ if (field.id && !embedded) { _%>
    @Id
    <%_ if (field.id && !embedded) { _%>
    @GeneratedValue(strategy = UNIQUE, delimiter = ID_DELIMITER)
    <%_ } _%>
  <%_ } else { _%>
    @Field
  <%_ } _%>
<&_ } -&>
<%_ } -%>

<%_ for (const field of fields.filter(field => field.fieldWithContentType)) { _%>
<&_ if (fragment.field<%- field.fieldNameCapitalized %>ContentTypeAnnotationSection) { -&>
    @Field
<&_ } -&>
<%_ } -%>

<%_/* for (const relationship of relationships) { _%>
<&_ if (fragment.relationship<%- relationship.relationshipNameCapitalized %>AnnotationSection) { -&>
  <%_ if (relationship.relationshipOneToMany) { _%>
    <%_ if (!relationship.otherEntity.embedded) { _%>
    @N1qlJoin(on = "lks.<%= relationship.relationshipFieldName %>=meta(rks).id", fetchType = FetchType.IMMEDIATE)
    <%_ } _%>

  <%_ } else if (relationship.relationshipManyToOne) { _%>
    <%_ if (!relationship.otherEntity.embedded) { _%>
    @N1qlJoin(on = "lks.<%= relationship.relationshipFieldName %>=meta(rks).id", fetchType = FetchType.IMMEDIATE)
    <%_ } _%>
  <%_ } else if (relationship.relationshipManyToMany) { _%>
    @N1qlJoin(on = "lks.<%= relationship.relationshipFieldNamePlural %>=meta(rks).id", fetchType = FetchType.IMMEDIATE)
  <%_ } else { _%>
    <%_ if (!relationship.otherEntity.embedded) { _%>
    @N1qlJoin(on = "lks.<%= relationship.relationshipFieldName %>=meta(rks).id", fetchType = FetchType.IMMEDIATE)
    <%_ } _%>
  <%_ } _%>

<&_ } -&>
<%_ } -%>
<% Commented out until N1qlJoin support is fixed */%>

<&_ if (fragment.classAdditionalRelationshipsSection) { -&>
    <%_ for (const relationship of relationships) { _%>
      <%_ if (!relationship.id && !relationship.otherEntity.embedded) { _%>
        <%_ if (relationship.collection) { _%>
    @JsonIgnore
    @Field("<%= relationship.relationshipFieldNamePlural %>")
    private Set<String> <%= relationship.relationshipFieldName %>Ids = new HashSet<>();
        <%_ } else if (relationship.ownerSide) { _%>
    @JsonIgnore
    @Field("<%= relationship.relationshipFieldName %>")
    private <%= relationship.otherEntity.primaryKey.type %> <%= relationship.relationshipFieldName %>Id;
        <%_ } _%>
      <%_ } _%>
    <%_ } _%>
<&_ } -&>

<%_ for (const relationship of relationships) { _%>
<&_ if (fragment.relationship<%- relationship.relationshipNameCapitalized %>CustomSetSection) { -&>
  <%_ if (relationship.collection) { _%>
    public void set<%= relationship.relationshipNameCapitalizedPlural %>(Set<<%= relationship.otherEntity.persistClass %>> <%= relationship.otherEntity.entityInstancePlural %>) {
    <%_ if (relationship.otherRelationship && !relationship.ownerSide) { _%>
        if (this.<%= relationship.relationshipFieldNamePlural %> != null) {
      <%_ if (relationship.otherRelationship.collection) { _%>
            this.<%= relationship.relationshipFieldNamePlural %>.forEach(i -> i.remove<%= relationship.otherRelationship.relationshipNameCapitalized %>(this));
      <%_ } else { _%>
            this.<%= relationship.relationshipFieldNamePlural %>.forEach(i -> i.set<%= relationship.otherRelationship.relationshipNameCapitalized %>(null));
      <%_ } _%>
        }
        if (<%= relationship.otherEntity.entityInstancePlural %> != null) {
      <%_ if (relationship.otherRelationship.collection) { _%>
            <%= relationship.otherEntity.entityInstancePlural %>.forEach(i -> i.add<%= relationship.otherRelationship.relationshipNameCapitalized %>(this));
      <%_ } else { _%>
            <%= relationship.otherEntity.entityInstancePlural %>.forEach(i -> i.set<%= relationship.otherRelationship.relationshipNameCapitalized %>(this));
      <%_ } _%>
        }
    <%_ } _%>
        this.<%= relationship.relationshipFieldNamePlural %> = <%= relationship.otherEntity.entityInstancePlural %>;
    <%_ if (!relationship.otherEntity.embedded) { _%>
        this.<%= relationship.relationshipFieldName %>Ids = <%= relationship.otherEntity.entityInstancePlural %>.stream()
            .map(<%= relationship.otherEntity.persistClass %>::getId)
            .collect(Collectors.toSet());
    <%_ } _%>
    }
  <%_ } else if (!relationship.otherEntity.embedded) { _%>
    public void set<%= relationship.relationshipNameCapitalized %>(<%= relationship.otherEntity.persistClass %> <%= relationship.otherEntityName %>) {
    <%_ if (relationship.otherRelationship && !relationship.ownerSide) { _%>
        if (this.<%= relationship.relationshipFieldName %> != null) {
      <%_ if (relationship.otherRelationship.collection) { _%>
            this.<%= relationship.relationshipFieldName %>.remove<%= relationship.otherRelationship.relationshipNameCapitalized %>(this);
      <%_ } else { _%>
            this.<%= relationship.relationshipFieldName %>.set<%= relationship.otherRelationship.relationshipNameCapitalized %>(null);
      <%_ } _%>
        }
        if (<%= relationship.otherEntityName %> != null) {
      <%_ if (relationship.otherRelationship.collection) { _%>
            <%= relationship.otherEntityName %>.add<%= relationship.otherRelationship.relationshipNameCapitalized %>(this);
      <%_ } else { _%>
            <%= relationship.otherEntityName %>.set<%= relationship.otherRelationship.relationshipNameCapitalized %>(this);
      <%_ } _%>
        }
    <%_ } _%>
        this.<%= relationship.relationshipFieldName %> = <%= relationship.otherEntityName %>;
        this.<%= relationship.relationshipFieldName %>Id = <%= relationship.otherEntityName %> != null ? <%= relationship.otherEntityName %>.get<%= relationship.otherEntity.primaryKey.nameCapitalized %>() : null;
    }
  <%_ } _%>
<&_ } -&>
<%_ } _%>

<%_ for (const relationship of relationships) { _%>
<&_ if (fragment.relationship<%- relationship.relationshipNameCapitalized %>CustomAddSection) { -&>
    public <%= persistClass %> add<%= relationship.relationshipNameCapitalized %>(<%= relationship.otherEntity.persistClass %> <%= relationship.otherEntityName %>) {
        this.<%= relationship.relationshipFieldNamePlural %>.add(<%= relationship.otherEntityName %>);
      <%_ if (!relationship.otherEntity.embedded || embedded && relationship.ownerSide) { _%>
        <%_ if (!relationship.otherEntity.embedded) { _%>
        this.<%= relationship.relationshipFieldName %>Ids.add(<%= relationship.otherEntityName %>.get<%= relationship.otherEntity.primaryKey.nameCapitalized %>());
        <%_ } _%>
        <%_ if (relationship.otherRelationship) { _%>
          <%_ if (!relationship.otherRelationship.collection) { _%>
        <%= relationship.otherEntityName %>.set<%= relationship.otherRelationship.relationshipNameCapitalized %>(this);
          <%_ } else if (relationship.otherRelationship.relationshipNamePlural && !relationship.otherEntityUser && relationship.otherRelationship.collection) { _%>
        <%= relationship.otherEntityName %>.get<%= relationship.otherRelationship.relationshipNameCapitalizedPlural %>().add(this);
          <%_ } _%>
        <%_ } _%>
      <%_ } _%>
        return this;
    }
<&_ } -&>
<%_ } _%>

<%_ for (const relationship of relationships) { _%>
<&_ if (fragment.relationship<%- relationship.relationshipNameCapitalized %>CustomRemoveSection) { -&>
    public <%= persistClass %> remove<%= relationship.relationshipNameCapitalized %>(<%= relationship.otherEntity.persistClass %> <%= relationship.otherEntityName %>) {
        this.<%= relationship.relationshipFieldNamePlural %>.remove(<%= relationship.otherEntityName %>);
      <%_ if (!relationship.otherEntity.embedded || (embedded && relationship.ownerSide)) { _%>
        <%_ if (!relationship.otherEntity.embedded) { _%>
        this.<%= relationship.relationshipFieldName %>Ids.remove(<%= relationship.otherEntityName %>.get<%= relationship.otherEntity.primaryKey.nameCapitalized %>());
        <%_ } _%>
        <%_ if (relationship.otherRelationship) { _%>
          <%_ if (!relationship.otherRelationship.collection) { _%>
        <%= relationship.otherEntityName %>.set<%= relationship.otherRelationship.relationshipNameCapitalized %>(null);
          <%_ } else if (!relationship.otherEntityUser && relationship.otherRelationship.collection) { _%>
        <%= relationship.otherEntityName %>.get<%= relationship.otherRelationship.relationshipNameCapitalizedPlural %>().remove(this);
          <%_ } _%>
        <%_ } _%>
      <%_ } _%>
        return this;
    }
<&_ } -&>
<%_ } _%>
